package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EsStockDamageReport;
import com.ruoyi.system.service.IEsStockDamageReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存报损单(es_stock_damage_report)Controller
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/system/report")
public class EsStockDamageReportController extends BaseController
{
    @Autowired
    private IEsStockDamageReportService esStockDamageReportService;

    /**
     * 查询库存报损单(es_stock_damage_report)列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsStockDamageReport esStockDamageReport)
    {
        startPage();
        List<EsStockDamageReport> list = esStockDamageReportService.selectEsStockDamageReportList(esStockDamageReport);
        return getDataTable(list);
    }

    /**
     * 导出库存报损单(es_stock_damage_report)列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsStockDamageReport esStockDamageReport)
    {
        List<EsStockDamageReport> list = esStockDamageReportService.selectEsStockDamageReportList(esStockDamageReport);
        ExcelUtil<EsStockDamageReport> util = new ExcelUtil<EsStockDamageReport>(EsStockDamageReport.class);
        util.exportExcel(response, list, "库存报损单(es_stock_damage_report)数据");
    }

    /**
     * 获取库存报损单(es_stock_damage_report)详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(esStockDamageReportService.selectEsStockDamageReportById(id));
    }

    /**
     * 新增库存报损单(es_stock_damage_report)
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsStockDamageReport esStockDamageReport)
    {
        return toAjax(esStockDamageReportService.insertEsStockDamageReport(esStockDamageReport));
    }

    /**
     * 修改库存报损单(es_stock_damage_report)
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsStockDamageReport esStockDamageReport)
    {
        return toAjax(esStockDamageReportService.updateEsStockDamageReport(esStockDamageReport));
    }

    /**
     * 删除库存报损单(es_stock_damage_report)
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esStockDamageReportService.deleteEsStockDamageReportByIds(ids));
    }

    /**
     * 提交库存报损单
     */
    @PreAuthorize("@ss.hasPermi('system:report:submit')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{id}")
    public AjaxResult submit(@PathVariable Long id) {
        EsStockDamageReport report = esStockDamageReportService.selectEsStockDamageReportById(id);
        if (report == null) {
            return error("报损单不存在");
        }
        report.setStatus(1L); // 设置为待审核状态
        return toAjax(esStockDamageReportService.updateEsStockDamageReport(report));
    }

    /**
     * 审核库存报损单
     */
    @PreAuthorize("@ss.hasPermi('system:report:audit')")
    @Log(title = "库存报损单(es_stock_damage_report)", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{id}")
    public AjaxResult audit(@PathVariable Long id) {
        EsStockDamageReport report = esStockDamageReportService.selectEsStockDamageReportById(id);
        if (report == null) {
            return error("报损单不存在");
        }
        report.setStatus(2L); // 设置为审核通过状态
        return toAjax(esStockDamageReportService.updateEsStockDamageReport(report));
    }

    /**
     * 获取报损类型列表
     */
    @GetMapping("/damageTypes")
    public AjaxResult getDamageTypes() {
        List<Map<String, String>> damageTypes = esStockDamageReportService.getDamageTypes();
        return success(damageTypes);
    }

    @GetMapping("/goodsList")
    public TableDataInfo goodsList(@RequestParam Long warehouseId,
                                   @RequestParam(required = false) String keyword) {
        startPage();
        List<Map<String, Object>> list = esStockDamageReportService.selectGoodsListWithStock(warehouseId, keyword);
        return getDataTable(list);
    }
}
